package view;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import adapter.PaperStructureAdapter;
import bean.AutoTestPaperBean;
import bean.SingleTypeQuestionBean;
import student.andlei.com.student.R;
public class PaperStructurePopupWindow extends PopupWindow implements View.OnClickListener {

    private Context context;
    private RecyclerView rcPaperStructure;
    private TextView tvCancel;
    private TextView tvOk;
    private TextView tvTotalScore;
    private List<AutoTestPaperBean.mlist> singleTypeQuestionBeanList;
    private List<AutoTestPaperBean> paperBeanList;
    public interface OnChangePaperTypeStructureListener {
        void onPaperTypeStructureChanged(List<AutoTestPaperBean> singleTypeQuestionBeanList);
    }

    private OnChangePaperTypeStructureListener onChangePaperTypeStructureListener;

    public PaperStructurePopupWindow(Context context, List<AutoTestPaperBean> list, OnChangePaperTypeStructureListener onChangePaperTypeStructureListener) {
        super(context);
        this.context = context;
        this.paperBeanList = list;
        this.onChangePaperTypeStructureListener = onChangePaperTypeStructureListener;
        initalize(list);
    }

    private void initalize(final List<AutoTestPaperBean> list) {
        initList(list);


        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.popup_paper_structure, null);

        rcPaperStructure = (RecyclerView) view.findViewById(R.id.rc_paper_structure);
        tvTotalScore = view.findViewById(R.id.tv_paper_total_score);
        tvCancel = (TextView) view.findViewById(R.id.tv_cancel);
        tvOk = (TextView) view.findViewById(R.id.tv_ok);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false);
        rcPaperStructure.setLayoutManager(linearLayoutManager);
        final PaperStructureAdapter paperStructureAdapter = new PaperStructureAdapter(R.layout.item_single_paper_structure, paperBeanList,tvTotalScore);
        paperStructureAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                if (singleTypeQuestionBeanList != null) {
                    switch (view.getId()) {
                        case R.id.iv_up:
                            Collections.swap(paperBeanList, position, position - 1);
                            paperStructureAdapter.notifyDataSetChanged();
                            break;
                        case R.id.iv_down:
                            if(paperBeanList.size()<=1){return;}
                            Collections.swap(paperBeanList, position, position + 1);
                            paperStructureAdapter.notifyDataSetChanged();
                            break;
                    }
                }

            }
        });
        paperStructureAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                //点击条目关闭软键盘
                InputMethodManager inputMgr = (InputMethodManager) context
                        .getSystemService(Context.INPUT_METHOD_SERVICE);
                if (view.getWindowToken() != null) {
                    inputMgr.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
                }
            }
        });
        rcPaperStructure.setAdapter(paperStructureAdapter);

        tvCancel.setOnClickListener(this);
        tvOk.setOnClickListener(this);

        setContentView(view);
        initWindow();
    }

    private void initList(List<AutoTestPaperBean> list) {
        singleTypeQuestionBeanList = new ArrayList<>();
        int tempTypeId = 0;
        SingleTypeQuestionBean tempTypeBean = null;
        for (AutoTestPaperBean singleQuestionBean : list) {
            int type_id = singleQuestionBean.type_id;
            if (type_id != tempTypeId) {
                tempTypeBean = new SingleTypeQuestionBean();
                tempTypeBean.setType_id(type_id);
                tempTypeBean.setType_name(singleQuestionBean.type_name);
//                tempTypeBean.getSingleQuestionList().add(singleQuestionBean);
//                singleTypeQuestionBeanList.add(tempTypeBean);
                tempTypeId = type_id;
            } else {
//                tempTypeBean.getSingleQuestionList().add(singleQuestionBean);
            }
        }
    }

    private void initWindow() {
        DisplayMetrics d = context.getResources().getDisplayMetrics();
        int width = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 373, context.getResources().getDisplayMetrics());
        int height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 480, context.getResources().getDisplayMetrics());
        this.setWidth(width);
        this.setHeight(height);
        //外部点击不能关闭
        this.setOutsideTouchable(true);
        this.setFocusable(true);
        this.update();
        //实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0x00000000);
        //设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
        backgroundAlpha((Activity) context, 0.8f);//0.0-1.0
        this.setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {
                backgroundAlpha((Activity) context, 1f);
            }
        });

//        this.setTouchInterceptor(new View.OnTouchListener() {
//            @Override
//            public boolean onTouch(View v, MotionEvent event) {
//                int height = v.getHeight();
//                float touchY = event.getY();
//                if (touchY > 0 && touchY < height) {
//
//                }
//                else {
//                    MorePopOutSideTouchable(v, event);	//自己写方法进行处理
//                return false;    //点击pop外部是 进行拦截 不让事件传递下去
////                }
//            }
//        });

    }

    //设置添加屏幕的背景透明度
    public void backgroundAlpha(Activity context, float bgAlpha) {
        WindowManager.LayoutParams lp = context.getWindow().getAttributes();
        lp.alpha = bgAlpha;
        context.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        context.getWindow().setAttributes(lp);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_cancel:
                dismiss();
                break;
            case R.id.tv_ok:
                if (onChangePaperTypeStructureListener != null) {
                    onChangePaperTypeStructureListener.onPaperTypeStructureChanged(paperBeanList);
                }
                dismiss();
                break;
        }
    }
}
